package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.entity.Region;
import com.yuelao.yuelao_backend.service.RegionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 地区Controller
 */
@RestController
@RequestMapping("/regions")
@Slf4j
@RequiredArgsConstructor
public class RegionController {

    private final RegionService regionService;

    /**
     * 获取所有省份
     * GET /regions/provinces
     * @return 省份列表
     */
    @GetMapping("/provinces")
    public ApiResponse<List<Region>> getProvinces() {
        List<Region> provinces = regionService.getProvinces();
        return ApiResponse.ok(provinces);
    }

    /**
     * 获取指定省份下的城市
     * GET /regions/cities
     * @param provinceCode 省份代码
     * @return 城市列表
     */
    @GetMapping("/cities")
    public ApiResponse<List<Region>> getCities(@RequestParam String provinceCode) {
        List<Region> cities = regionService.getCities(provinceCode);
        return ApiResponse.ok(cities);
    }

    /**
     * 获取指定城市下的区县
     * GET /regions/districts
     * @param cityCode 城市代码
     * @return 区县列表
     */
    @GetMapping("/districts")
    public ApiResponse<List<Region>> getDistricts(@RequestParam String cityCode) {
        List<Region> districts = regionService.getDistricts(cityCode);
        return ApiResponse.ok(districts);
    }

    /**
     * 获取完整的三级联动数据
     * GET /regions/all
     * @return 完整地区数据
     */
    @GetMapping("/all")
    public ApiResponse<Map<String, Object>> getAllRegions() {
        Map<String, Object> result = regionService.getAllRegions();
        return ApiResponse.ok(result);
    }
}

